import { Controller } from "../../mvc/core/Controller";
import { IView } from "../../mvc/interfaces/IView";
import { DialogTaskModule } from "../../../modules/DialogTaskModule";
import { EventName } from "../mvc/EventName";
import { WindowEvent } from "../../component/window/WindowEvent";
import { DataEvent } from "../events/DataEvent";
import { NPCInfo } from "../model/NPCInfo";
import { GameMapUtil } from "../scene/map/GameMapUtil";
import { RolePlayer } from "../scene/player/entity/RolePlayer";
import { PlayerEvent } from "../scene/events/PlayerEvent";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { NpcEffectRule } from "../rules/NpcEffectRule";
import { ENpcEffect } from "../../../Message/Public/ENpcEffect";
import { ETaskGroup } from "../../../Message/Game/ETaskGroup";
import { ETaskStatus } from "../../../Message/Game/ETaskStatus";
import { GameController } from "../mvc/GameController";
import { GameManager } from "../manager/GameManager";
import { ModuleType } from "../view/common/ModuleType";
import { Cache } from "../cache/Cache";
import { GameProxy } from "../mvc/GameProxy";
import { DialogInfo } from "../model/dialog/DialogInfo";
import { DialogOperate } from "../rules/DialogOperate";
import { ETaskType } from "../../../Message/Game/ETaskType";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { EGateCommand } from "../../../Message/Command/EGateCommand";
import { ECmdCell } from "../../../Message/Command/ECmdCell";
import { OperateInfo } from "../model/OperateInfo";
import { GuideOptType } from "../rules/GuideOptType";
import { GuideType } from "../rules/GuideType";
import { SoundManager } from "../../common/sound/SoundManager";
import { SoundNameConst } from "../../common/sound/SoundNameConst";
import { Alert } from "../../../com/mui/controls/Alert";
import { GameConst } from "../../component/gconst/GameConst";
import { EPassType } from "../../../Message/Game/EPassType";
import { EEntityFightMode } from "../../../Message/Public/EEntityFightMode";
import { SceneConfig } from "../resource/SceneConfig";
import { GameDefConfig } from "../resource/GameDefConfig";
import { ClockManager } from "../manager/ClockManager";
import { IDialogModule } from "../../../modules/interfaces/IDialogModule";
import { Log } from "../../../com/gengine/debug/Log";
import { ECareer } from "../../../Message/Public/ECareer";
import { TransportInfo } from "../model/TransportInfo";
import { DialogGuideNextShowingOpData } from "../view/dialog/data/DialogGuideNextShowingOpData";
import { GuildTaskInfo } from "../model/GuildTaskInfo";
import { SGuildFlameInfo } from "../../../Message/Game/SGuildFlameInfo";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { SStory } from "../../../Message/Public/SStory";
import { ThingUtil } from "../scene/layer/utils/ThingUtil";
type int = number;
//class DialogController
    
    export  class DialogController extends Controller
    {
       

        protected /* override */ initView(): IView
        {
            var loc1=new DialogTaskModule();
            loc1.addEventListener(EventName.NpcTaskInfoReq, this.onTaskInfoReq);
            loc1.addEventListener(EventName.NpcTaskDoReq, this.onTaskDoReq);
            loc1.addEventListener(EventName.NpcOptReq, this.onNpcOptReq);
            loc1.addEventListener(WindowEvent.CLOSE, this.onWindowClose);
            loc1.addEventListener(WindowEvent.SHOW, this.onWindowShow);
            return loc1;
        }

        /* internal  */onInNpcDestance(arg1: DataEvent): void
        {
            var loc1=arg1.data as NPCInfo;
            cache.dialog.npcInfo = loc1;
            if (GameMapUtil.curMapState.isCrossCountry) 
            {
                cache.dialog.updateNpcOperate(loc1.tnpc.effectCross);
            }
            else 
            {
                cache.dialog.updateNpcOperate(loc1.tnpc.effectDes);
            }
            this.taskListReq();
            RolePlayer.instance.addEventListener(PlayerEvent.GIRD_WALK_END, this.checkOutDistance);
            Dispatcher.addEventListener(EventName.Convey_Res, this.checkOutDistance);
            Dispatcher.addEventListener(EventName.ChangeScene, this.checkOutDistance);
            return;
        }

        /* internal  */taskListReq(): void
        {
            var loc1=false;
            var loc2=false;
            var loc3=false;
            var loc4=false;
            if (NpcEffectRule.hasNpcEffect(cache.dialog.operateList, ENpcEffect._ENpcEffectShengTianIncome)) 
            {
                Dispatcher.dispatchEvent(new Object /* flash.events.Event */(EventName.CrossMainCity_IncomeView_Open));
                return;
            }
            if (NpcEffectRule.hasNpcEffect(cache.dialog.operateList, ENpcEffect._ENpcEffectLGXB)) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.LGXBOpeng));
                return;
            }
            if (NpcEffectRule.hasNpcEffect(cache.dialog.operateList, ENpcEffect._ENpcEffectCopy129)) 
            {
                loc1 = cache.task.hasTaskByGroup(ETaskGroup._ETaskGroupFairyland, true, ETaskStatus._ETaskStatusHadCompleted);
                loc2 = cache.task.hasTaskByGroup(ETaskGroup._ETaskGroupCrossSea, true, ETaskStatus._ETaskStatusHadCompleted);
                if (!loc1 || !loc2) 
                {
                    loc3 = cache.task.hasTaskByGroup(ETaskGroup._ETaskGroupFairyland, false, -1);
                    loc4 = cache.task.hasTaskByGroup(ETaskGroup._ETaskGroupCrossSea, false, -1);
                    if (loc3 || loc4) 
                    {
                        Dispatcher.dispatchEvent(new Object /* flash.events.Event */(EventName.FairylandOpenTaskPanel));
                    }
                    else 
                    {
                        Dispatcher.dispatchEvent(new Object /* flash.events.Event */(EventName.FairylandCloseTaskPanel));
                        if (GameController.copy.crossSea.isOpening) 
                        {
                            Dispatcher.dispatchEvent(new DataEvent(EventName.CrossSeaShowEnterWinReq));
                        }
                        else 
                        {
                            GameManager.instance.popupWindow(ENpcEffect._ENpcEffectCopy129);
                        }
                    }
                    return;
                }
            }
            if (this.checkCanShowSecondCareerGetTaskPanel()) 
            {
                return;
            }
            if (cache.guide.guideSkillLearn) 
            {
                GameManager.instance.popupWindow(ModuleType.Skills);
                return;
            }
            if (cache.guide.petGuide && !cache.guide.petBattle) 
            {
                GameManager.instance.popupWindow(ModuleType.Pets);
                return;
            }
            if (cache.guide.isGuideNpcEffect(cache.dialog.npcInfo.tnpc.npcId)) 
            {
                this._operateInfo = cache.dialog.operateList[0];
                if (this._operateInfo != null) 
                {
                    GameManager.instance.popupWindow(this._operateInfo.operateId);
                    this._operateInfo = null;
                    return;
                }
            }
            if (cache.guide.guideApplyGuild) 
            {
                GameController.guild.guildListModule.show();
                return;
            }
            if (cache.guide.guideUseDrug) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.Guide_UseDrug));
                return;
            }
            if (cache.guide.guideEquipRefresh && cache.guide.isGuideEquipRefresh(cache.dialog.npcInfo.tnpc.npcId)) 
            {
                GameManager.instance.popupWindow(ModuleType.Build);
                return;
            }
            if (cache.guide.guidePetMerge && cache.guide.isGuidePetMerge(cache.dialog.npcInfo.tnpc.npcId)) 
            {
                GameManager.instance.popupWindow(ModuleType.Pets);
                return;
            }
            if (cache.guide.guidePetRefresh && cache.guide.isGuidePetRefresh(cache.dialog.npcInfo.tnpc.npcId)) 
            {
                GameController.pet.view.show();
                return;
            }
            if (NpcEffectRule.hasNpcEffect(cache.dialog.operateList, ENpcEffect._ENpcEffectArena)) 
            {
                if (Cache.instance.wrestle.isCross() || Cache.instance.wrestle.isOpen) 
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.wrestleShowRoomWindow));
                    return;
                }
            }
            if (cache.scene.isGetRunBussinessNpc(cache.dialog.npcInfo.tnpc.npcId)) 
            {
                cache.dialog.dialogInfo = null;
                this.onTaskListRes([]);
            }
            else if (NpcEffectRule.hasNpcEffect(cache.dialog.operateList, ENpcEffect._ENpcEffectCareerChange)) 
            {
                GameProxy.career.openFirstChangeCareerTask(cache.dialog.npcInfo.tnpc.npcId);
            }
            else 
            {
                GameProxy.dialogProxy.npcTaskReq(cache.dialog.npcInfo.tnpc.npcId);
            }
            return;
        }

        /* internal  */checkCanShowSecondCareerGetTaskPanel(): Boolean
        {
            if (!NpcEffectRule.hasNpcEffect(cache.dialog.operateList, ENpcEffect._ENpcEffectSecondCareer)) 
            {
                return false;
            }
            if (!cache.secondCareer.canGetOpenSecondCareerTask()) 
            {
                return false;
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.SecondCareerShowGetTaskPanel));
            return true;
        }

        /* internal  */onTaskInfoReq(arg1: DataEvent): void
        {
            var loc2=null;
            var loc1=arg1.data as DialogInfo;
            if (loc1.operateId == DialogOperate.GetTask || loc1.status == ETaskStatus._ETaskStatusCanGet) 
            {
                this.onTaskInfoRes(loc1);
            }
            else 
            {
                loc2 = cache.task.getDoingTaskByCode(loc1.sTask.code);
                if (loc2 != null) 
                {
                    if (loc2.sTask.group != ETaskGroup._ETaskGroupTransport) 
                    {
                        loc1 = cache.dialog.updateTaskInfo(loc2.sTask);
                        this.onTaskInfoRes(loc1);
                    }
                    else 
                    {
                        GameProxy.transport.transportTaskInfoReq(cache.dialog.npcInfo.tnpc.npcId);
                    }
                }
            }
            return;
        }

        /* internal  */onTaskDoReq(arg1: DataEvent): void
        {
            var loc1=cache.dialog.dialogInfo;
            if (loc1.operateId == DialogOperate.DoTask) 
            {
                GameProxy.taskProxy.doTaskReq(loc1.sTask.code, cache.dialog.npcInfo.tnpc.npcId, loc1.code);
            }
            return;
        }

        /* internal  */onTaskEndReq(arg1: DataEvent): void
        {
            var loc1=cache.dialog.dialogInfo;
            if (loc1) 
            {
                if (loc1.operateId != DialogOperate.ListTask) 
                {
                    if (cache.guidePetLifeStyle.isGuideDressLifeStyle) 
                    {
                        if (loc1.sTask.type == ETaskType._ETaskTypeLifestyleDress) 
                        {
                            MsgManager.showRollTipsMsg(Language.getString(27208));
                            Dispatcher.dispatchEvent(new DataEvent(EventName.Guide_PetLifeStyleDress));
                            return;
                        }
                    }
                    if (loc1.sTask.group != ETaskGroup._ETaskGroupGuild) 
                    {
                        if (loc1.sTask.group != ETaskGroup._ETaskGroupCycle) 
                        {
                            GameProxy.taskProxy.endTaskReq(cache.dialog.npcInfo.tnpc.npcId, loc1.sTask.code, loc1.sTask.endChoose);
                        }
                        else 
                        {
                            GameProxy.centralTask.endCycleTask(cache.dialog.npcInfo.tnpc.npcId, loc1.sTask.code);
                        }
                    }
                    else 
                    {
                        GameProxy.guildTask.endTaskReq(cache.dialog.npcInfo.tnpc.npcId, loc1.sTask.code);
                    }
                }
                else if (NpcEffectRule.hasNpcEffect(cache.dialog.operateList, ENpcEffect._ENpcGuildGetRunBusiness) && cache.task.hasTaskByGroup(ETaskGroup._ETaskGroupRunBusiness, true)) 
                {
                    GameProxy.dialogProxy.npcTaskReq(cache.dialog.npcInfo.tnpc.npcId);
                }
                else 
                {
                    this.onTaskInfoRes(loc1);
                }
            }
            else 
            {
                GameProxy.dialogProxy.npcTaskReq(cache.dialog.npcInfo.tnpc.npcId);
            }
            return;
        }

        /* internal  */onTaskGetReq(arg1: DataEvent): void
        {
            var loc1=cache.dialog.dialogInfo;
            if (!loc1 || cache.dialog.taskList.length == 0 && loc1.operateId == DialogOperate.ListTask) 
            {
                if (NpcEffectRule.hasNpcEffect(cache.dialog.operateList, ENpcEffect._ENpcGuildTask) && cache.daily.canGetGuildTask()) 
                {
                    GameProxy.guildTask.openGuildTask(cache.dialog.npcInfo.tnpc.npcId);
                }
                else if (NpcEffectRule.hasNpcEffect(cache.dialog.operateList, ENpcEffect._ENpcEffectHuntTask) && cache.daily.canGetHuntTask()) 
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.HuntDialogShow));
                    view.hide();
                }
                else if (NpcEffectRule.hasNpcEffect(cache.dialog.operateList, ENpcEffect._ENpcGuildGetRunBusiness) && cache.task.hasTaskByGroup(ETaskGroup._ETaskGroupRunBusiness, false)) 
                {
                    GameProxy.dialogProxy.npcTaskReq(cache.dialog.npcInfo.tnpc.npcId);
                }
            }
            else if (loc1.operateId != DialogOperate.ListTask) 
            {
                if (loc1.sTask.group != ETaskGroup._ETaskGroupGuild) 
                {
                    GameProxy.taskProxy.getTaskReq(cache.dialog.npcInfo.tnpc.npcId, loc1.sTask.code);
                }
                else 
                {
                    GameProxy.guildTask.getTaskReq(cache.dialog.npcInfo.tnpc.npcId, loc1.sTask.code);
                }
            }
            else 
            {
                this.onTaskInfoRes(loc1);
            }
            return;
        }

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.AI_NpcInDestance, this.onInNpcDestance);
            Dispatcher.addEventListener(EventName.Scene_Update, this.onSceneUpdateHandler);
            Dispatcher.addEventListener(EventName.NpcUpdateDialogInfo, this.onUpdateDialogTaskInfo);
            Dispatcher.addEventListener(EventName.NpcDialogTaskInfoPanelShowHide, this.hideOrShowDialogPanelHandler);
            Dispatcher.addEventListener(EventName.NpcTaskEndReq, this.onTaskEndReq);
            Dispatcher.addEventListener(EventName.NpcTaskGetReq, this.onTaskGetReq);
            Dispatcher.addEventListener(EventName.NpcGuidNextShwoingOpList, this.guideNextShowingOpListHandler);
            Dispatcher.addEventListener(EventName.NpcGuildTaskQuickCompleteReq, this.reqGuildTaskQuickComHandler);
            Dispatcher.addEventListener(EventName.EatParadeProcess, this.eatParadeNpcDialogHide);
            NetDispatcher.addCmdListener(ServerCommand.DialogTaskListRes, this.onTaskListRes);
            NetDispatcher.addCmdListener(ServerCommand.DialogTaskInfoRes, this.onTaskInfoRes);
            NetDispatcher.addCmdListener(ServerCommand.TaskGetRes, this.onTaskGetRes);
            NetDispatcher.addCmdListener(ServerCommand.TaskEndRes, this.onTaskEndRes);
            NetDispatcher.addCmdListener(ServerCommand.TransportTalkRes, this.onTransportTalkRes);
            NetDispatcher.addCmdListener(ServerCommand.DialogTransportTaskInfoRes, this.onTransportTaskInfoRes);
            NetDispatcher.addCmdListener(ServerCommand.CanGetListAdd, this.onCanGetTaskAddRes);
            NetDispatcher.addCmdListener(ServerCommand.CanGetListRefresh, this.onCanGetTaskAddRes);
            NetDispatcher.addCmdListener(ServerCommand.TaskUpdate, this.onTaskUpdataRes);
            NetDispatcher.addCmdListener(ServerCommand.GuildTaskInfoRes, this.onGuildTaskInfoRes);
            NetDispatcher.addCmdListener(ServerCommand.GuildFlameInfoUpdate, this.onGuildFlameInfoRes);
            NetDispatcher.addCmdListener(ServerCommand.GuildWarCopyInfosUpdate, this.onGuildWarCopyInfosUpdate);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateKillByEntity, this.onRoleKillByEntity);
            NetDispatcher.addCmdListener(ECmdCell._ECmdCellMagicTowerQuestion, this.onCopyMagicTowerQuestion);
            return;
        }

        /* internal  */onNpcOptReq(arg1: DataEvent): void
        {
            var loc1=null;
            this._operateInfo = arg1.data as OperateInfo;
            if (this._operateInfo.operateId != ENpcEffect._ENpcEffectPass) 
            {
                if (this._operateInfo.operateId != ENpcEffect._ENpcEffectTransport) 
                {
                    if (this._operateInfo.operateId != ENpcEffect._ENpcGuildTask) 
                    {
                        if (this._operateInfo.operateId != GuideOptType.GuideGoToGuildTaskBoss) 
                        {
                            if (this._operateInfo.operateId != ENpcEffect._ENpcEffectHuntTask) 
                            {
                                if (this._operateInfo.operateId != ENpcEffect._ENpcGuildGetRunBusiness) 
                                {
                                    if (this._operateInfo.operateId != GuideOptType.GuideCancelRunBuiness) 
                                    {
                                        if (this._operateInfo.operateId != ENpcEffect._ENpcGuildHandInItem) 
                                        {
                                            if (this._operateInfo.operateId == ENpcEffect._ENpcGuildFire) 
                                            {
                                                Dispatcher.dispatchEvent(new DataEvent(EventName.GuildFlameAddFireWood));
                                                return;
                                            }
                                            if (NpcEffectRule.isMusicPlay(this._operateInfo.operateId)) 
                                            {
                                                cache.guide.guideMusicPlay = true;
                                                if (cache.task.hasTaskByGuide(GuideType.GuideMusicPlay, ETaskStatus._ETaskStatusHadCompleted)) 
                                                {
                                                    this.taskListReq();
                                                }
                                                SoundManager.instance.musicPlay(SoundNameConst.getSoundNameByNpcEffect(this._operateInfo.operateId), 1);
                                            }
                                            else if (NpcEffectRule.isEatParadeId(this._operateInfo.operateId)) 
                                            {
                                                Dispatcher.dispatchEvent(new DataEvent(EventName.EatParadeApply));
                                            }
                                            else 
                                            {
                                                if (!view.isHide) 
                                                {
                                                    view.hide();
                                                }
                                                GameManager.instance.popupWindow(this._operateInfo.operateId);
                                                this._operateInfo = null;
                                            }
                                        }
                                        else 
                                        {
                                            GameProxy.dialogProxy.handInItemReq();
                                        }
                                    }
                                    else 
                                    {
                                        Alert.show(Language.getString(20305), null, Alert.OK | Alert.CANCEL, null, this.onCancelTaskAlertHandler);
                                    }
                                }
                                else 
                                {
                                    if (cache.role.entityInfo.level < GameConst.GuildCreatePlayerLevel) 
                                    {
                                        MsgManager.showRollTipsMsg(Language.getStringByParam(20303, GameConst.GuildCreatePlayerLevel));
                                        return;
                                    }
                                    if (cache.daily.runBussinessInfo.num >= cache.daily.runBussinessInfo.totalNum) 
                                    {
                                        if (!cache.task.hasTaskByGroup(ETaskGroup._ETaskGroupRunBusiness)) 
                                        {
                                            MsgManager.showRollTipsMsg(Language.getString(20304));
                                            return;
                                        }
                                    }
                                    GameProxy.dialogProxy.npcTaskReq(cache.dialog.npcInfo.tnpc.npcId);
                                }
                            }
                            else 
                            {
                                Dispatcher.dispatchEvent(new DataEvent(EventName.HuntDialogShow));
                                view.hide();
                            }
                        }
                        else 
                        {
                            GameProxy.sceneProxy.pass(EPassType.convert(EPassType._EPassTypeGuildTask), this._operateInfo.targetInfo.mapId, this._operateInfo.targetInfo.x, this._operateInfo.targetInfo.y);
                            view.hide();
                        }
                    }
                    else 
                    {
                        GameProxy.guildTask.openGuildTask(cache.dialog.npcInfo.tnpc.npcId);
                    }
                }
                else 
                {
                    GameProxy.transport.talkToTransportNPC(cache.dialog.npcInfo.tnpc.npcId);
                }
            }
            else 
            {
                if (cache.task.inTransport()) 
                {
                    MsgManager.showRollTipsMsg(Language.getString(20283));
                    return;
                }
                if (Cache.instance.role.entityInfo.level >= GameConst.SetFightModeLevel) 
                {
                    if (RolePlayer.instance.entityInfo.entityInfo.fightModel != EEntityFightMode._EEntityFightModePeace) 
                    {
                        this.onAlertHandler(Alert.OK);
                    }
                    else 
                    {
                        loc1 = SceneConfig.instance.getSceneInfo(this._operateInfo.passTo.mapId);
                        if (GameMapUtil.isEnemyMap(this._operateInfo.passTo.mapId)) 
                        {
                            Alert.show(Language.getStringByParam(20301, loc1.sMapDefine.name), null, Alert.OK | Alert.CANCEL, null, this.onAlertHandler);
                        }
                        else if (cache.nationalTrea.passMapCheckByMapId(this._operateInfo.passTo.mapId)) 
                        {
                            Alert.buttonWidth = 80;
                            Alert.show(Language.getStringByParam(27062, loc1.sMapDefine.name), Language.getString(20055), Alert.OK | Alert.CANCEL, null, this.onAlertHandler);
                        }
                        else 
                        {
                            this.onAlertHandler(Alert.OK);
                        }
                    }
                }
                else if (GameMapUtil.isEnemyMap(this._operateInfo.passTo.mapId)) 
                {
                    Alert.show(Language.getStringByParam(20302, GameConst.SetFightModeLevel));
                }
                else 
                {
                    this.onAlertHandler(Alert.OK);
                }
            }
            return;
        }

        /* internal  */onAlertHandler(arg1: int): void
        {
            if (arg1 == Alert.OK) 
            {
                if (!view.isHide) 
                {
                    view.hide();
                }
                GameProxy.sceneProxy.pass(EPassType.convert(EPassType._EPassTypeNpc), cache.dialog.npcInfo.tnpc.npcId, this._operateInfo.passTo.passToId);
            }
            this._operateInfo = null;
            return;
        }

        /* internal  */onCancelTaskAlertHandler(arg1: int): void
        {
            if (arg1 == Alert.OK) 
            {
                if (!view.isHide) 
                {
                    view.hide();
                }
                GameProxy.taskProxy.cancelRunBuinessTask();
            }
            this._operateInfo = null;
            return;
        }

        /* internal  */onTaskListRes(arg1: Array<any>): void
        {
            var loc2=null;
            var loc3=false;
            var loc4=null;
            var loc5=null;
            var loc6=0;
            var loc7=null;
            var loc8=null;
            if (NpcEffectRule.hasNpcEffect(cache.dialog.operateList, ENpcEffect._ENpcEffectCopy129)) 
            {
                if (!(loc3 = cache.task.hasTaskByGroup(ETaskGroup._ETaskGroupFairyland, true, ETaskStatus._ETaskStatusHadCompleted))) 
                {
                    return;
                }
            }
            var loc1=0;
            if (arg1 != null) 
            {
                loc1 = arg1.length;
            }
            if (loc1 == 1) 
            {
                loc2 = arg1[0];
                if (loc2.sTask.group != ETaskGroup._ETaskGroupTransport) 
                {
                    GameProxy.dialogProxy.taskInfoReq(loc2.sTask.code);
                    if (loc2.sTask.group == ETaskGroup._ETaskGroupTreasure) 
                    {
                        if (!view.isHide) 
                        {
                            view.hide();
                        }
                    }
                }
                else 
                {
                    GameProxy.transport.transportTaskInfoReq(cache.dialog.npcInfo.tnpc.npcId);
                }
                return;
            }
            if (loc1 == 0) 
            {
                if (NpcEffectRule.hasNpcEffect(cache.dialog.operateList, ENpcEffect._ENpcEffectMarryRegister)) 
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.MarryNpcOpen));
                    return;
                }
                if (cache.task.hasTaskByGroup(ETaskGroup._ETaskGroupTransport)) 
                {
                    if ((loc5 = cache.task.getTrasnportTask()) && loc5.sTask.endNpc == cache.dialog.npcInfo.tnpc.npcId) 
                    {
                        if (view.isHide) 
                        {
                            this.openView();
                        }
                        return;
                    }
                }
                if (!((loc4 = cache.dialog.operateList[0]) == null) && GameDefConfig.instance.exchangeEffectList.indexOf(loc4.operateId.toString()) > -1) 
                {
                    loc6 = 0;
                    while (loc6 < cache.dialog.operateList.length) 
                    {
                        if ((loc4 = cache.dialog.operateList[loc6]).endDate && loc4.endDate.time > ClockManager.instance.nowDate.time) 
                        {
                            if (!view.isHide) 
                            {
                                view.hide();
                            }
                            GameManager.instance.popupWindow(loc4.operateId);
                            return;
                        }
                        ++loc6;
                    }
                    return;
                }
                if (cache.dialog.operateList.length == 1 && !cache.dialog.isCopyNpc() && !NpcEffectRule.notEffectReq(loc4.operateId) && !NpcEffectRule.isEatParadeId(loc4.operateId)) 
                {
                    if ((loc8 = (loc7 = GameDefConfig.instance.getItem("MaskNPCOperateID", 1)).text1.split(",")).indexOf(loc4.operateId.toString()) > -1) 
                    {
                        MsgManager.showRollTipsMsg(Language.getString(200330));
                        return;
                    }
                    if (!view.isHide) 
                    {
                        view.hide();
                    }
                    GameManager.instance.popupWindow(loc4.operateId);
                    return;
                }
                if (NpcEffectRule.hasNpcEffect(cache.dialog.operateList, ENpcEffect._ENpcEffectCopyDigitalLevel)) 
                {
                    GameProxy.copy.getMagicTowerQuestion(false);
                    if (!view.isHide) 
                    {
                        view.hide();
                    }
                    return;
                }
                if (cache.dialog.operateList.length == 1 && loc4.operateId == ENpcEffect._ENpcEffectTransport) 
                {
                    GameProxy.transport.talkToTransportNPC(cache.dialog.npcInfo.tnpc.npcId);
                    if (!view.isHide) 
                    {
                        view.hide();
                    }
                    return;
                }
                if (cache.dialog.operateList.length >= 1 && loc4.operateId == ENpcEffect._ENpcGuildFire) 
                {
                    GameProxy.guildProxy.getFireWood(cache.guild.myGuildInfo.guildId);
                }
                else if (cache.dialog.operateList.length >= 1 && NpcEffectRule.hasNpcEffect(cache.dialog.operateList, ENpcEffect._ENpcGuildFightCurInfo)) 
                {
                    if (!view.isHide) 
                    {
                        view.hide();
                    }
                    GameProxy.guildWar.getGuildWarCopyInfos();
                    return;
                }
            }
            if (arg1 != null) 
            {
                this.checkShowAndUpdateDialog(cache.dialog.npcInfo, cache.dialog.getNPCTalk(), arg1, cache.dialog.operateList);
            }
            this.guideNextShowingOpList();
            return;
        }

        /* internal  */guideNextShowingOpList(): void
        {
            if (this._GuideNextShowingOpData == null) 
            {
                return;
            }
            if ((view as IDialogModule).guideShowingOpList(this._GuideNextShowingOpData) || this._GuideNextShowingOpData.isNextShowRelease) 
            {
                this._GuideNextShowingOpData = null;
            }
            return;
        }

        /* internal  */onTaskInfoRes(arg1: DialogInfo): void
        {
            if (cache.guide.selectedCareer && !cache.guide.selectedCareerRes) 
            {
                if (!view.isHide) 
                {
                    view.hide();
                }
                GameController.career.view.show();
                MsgManager.showRollTipsMsg(Language.getString(20306));
                return;
            }
            if (arg1.sTask == null) 
            {
                Log.debug("dialog.stask=null");
            }
            else 
            {
                cache.dialog.dialogInfo = arg1;
                if (arg1.sTask.guide & GuideType.UpdateCareer && !cache.guide.selectedCareer && cache.role.entityInfo.career == ECareer._ECareerNewPlayer && arg1.status == ETaskStatus._ETaskStatusHadCompleted) 
                {
                    if (!view.isHide) 
                    {
                        view.hide();
                    }
                    GameController.career.view.show();
                    MsgManager.showRollTipsMsg(Language.getString(20306));
                    return;
                }
                if (arg1.sTask.guide & GuideType.GuideMusicPlay && arg1.status == ETaskStatus._ETaskStatusHadCompleted && !cache.guide.guideMusicPlay) 
                {
                    this.checkShowAndUpdateDialog(cache.dialog.npcInfo, cache.dialog.getNPCTalk(), [], cache.dialog.operateList);
                    return;
                }
                if (arg1.sTask.group == ETaskGroup._ETaskGroupTreasure) 
                {
                    if (!view.isHide) 
                    {
                        view.hide();
                    }
                    Dispatcher.dispatchEvent(new DataEvent(EventName.HuntDialogShow));
                    return;
                }
                if (view.isHide) 
                {
                    this.openView();
                }
                (view as IDialogModule).updateTaskInfo(cache.dialog.npcInfo, arg1);
            }
            return;
        }

        /* internal  */onTransportTaskInfoRes(arg1: TransportInfo): void
        {
            if (view.isHide) 
            {
                this.openView();
            }
            (view as IDialogModule).updateTransportTaskInfo(cache.dialog.dialogInfo, cache.transport.transportInfo, cache.dialog.npcInfo);
            return;
        }

        /* internal  */onTaskGetRes(arg1: Object): void
        {
            if (arg1 == null) 
            {
                return;
            }
            if (!arg1.tasks || !arg1.task) 
            {
                return;
            }
            var loc1=arg1.tasks;
            var loc2=arg1.task;
            var loc3=arg1.npcId;
            if (cache.dialog.npcInfo && loc3 == cache.dialog.npcInfo.tnpc.npcId) 
            {
                if (cache.guide.guideShowPlot) 
                {
                    view.hide();
                    return;
                }
                if (loc2 && loc2.sTask.group == ETaskGroup._ETaskGroupGuild) 
                {
                    var loc4=loc2.sTask.type;
                    switch (loc4) 
                    {
                        case ETaskType._ETaskTypeCatchBoss: case ETaskType._ETaskTypeCollect: case ETaskType._ETaskTypeGroupKillBoss: case ETaskType._ETaskTypeKillBoss: case ETaskType._ETaskTypeKillBossCount: case ETaskType._ETaskTypeKillBossDrop: case ETaskType._ETaskTypeKillBossEx: case ETaskType._ETaskTypeKillBossDropEx: case ETaskType._ETaskTypeDeliverLetters: case ETaskType._ETaskTypeEscort:
                        {
                            if (GameMapUtil.curMapState.isCrossCountry) 
                            {
                                cache.dialog.updateNpcOperate(cache.dialog.npcInfo.tnpc.effectCross);
                            }
                            else 
                            {
                                cache.dialog.updateNpcOperate(cache.dialog.npcInfo.tnpc.effectDes);
                            }
                            this.checkShowAndUpdateDialog(cache.dialog.npcInfo, cache.dialog.getNPCTalk(), [], cache.dialog.operateList);
                            break;
                        }
                        default:
                        {
                            if (!_view.isHide) 
                            {
                                _view.hide();
                            }
                        }
                    }
                }
                else 
                {
                    cache.dialog.updateNpcTask(loc1);
                    this.onTaskChange(cache.dialog.taskList);
                }
            }
            return;
        }

        /* internal  */onTaskEndRes(arg1: Object): void
        {
            if (arg1 == null) 
            {
                view.hide();
                return;
            }
            var loc1=arg1.tasks;
            var loc2=arg1.task;
            var loc3=arg1.npcId;
            if (cache.dialog.npcInfo && loc3 == cache.dialog.npcInfo.tnpc.npcId) 
            {
                if (cache.guide.guideItemAlert) 
                {
                    view.hide();
                    return;
                }
                if (!view.isHide) 
                {
                    if (!cache.guide.guideItemAlert) 
                    {
                        (view as IDialogModule).endTaskRes();
                    }
                    if (!this.hasCanEndTask(loc1)) 
                    {
                        view.hide();
                    }
                }
                if (loc2 && loc2.sTask.group == ETaskGroup._ETaskGroupGuild) 
                {
                    if (GameMapUtil.curMapState.isCrossCountry) 
                    {
                        cache.dialog.updateNpcOperate(cache.dialog.npcInfo.tnpc.effectCross);
                    }
                    else 
                    {
                        cache.dialog.updateNpcOperate(cache.dialog.npcInfo.tnpc.effectDes);
                    }
                }
                cache.dialog.updateNpcTask(loc1);
                this.onTaskChange(cache.dialog.taskList, true);
            }
            return;
        }

        /* internal  */hasCanEndTask(arg1: Array<any>): Boolean
        {
            var loc1=null;
            var loc2=0;
            var loc3=arg1.length;
            while (loc2 < loc3) 
            {
                loc1 = arg1[loc2];
                if (loc1.status == ETaskStatus._ETaskStatusHadCompleted || loc1.status == ETaskStatus._ETaskStatusHadFail) 
                {
                    return true;
                }
                ++loc2;
            }
            return false;
        }

        /* internal  */onTransportTalkRes(arg1: TransportInfo): void
        {
            if (!arg1.transportAble) 
            {
                if (GameMapUtil.curMapState.isCrossCountry) 
                {
                    if (Cache.instance.dialog.getNPCTalk() == "" || Cache.instance.dialog.getNPCTalk() == null) 
                    {
                        return;
                    }
                }
                if (view.isHide) 
                {
                    this.openView();
                }
                (_view as IDialogModule).onTransportTalkRes(arg1, cache.dialog.npcInfo);
            }
            return;
        }

        /* internal  */onCanGetTaskAddRes(arg1: Array<any>): void
        {
            var loc1=0;
            var loc2=0;
            var loc3=null;
            if (_view && !_view.isHide) 
            {
                loc2 = arg1.length;
                while (loc1 < loc2) 
                {
                    if ((loc3 = arg1[loc1]).sTask.getNpc == cache.dialog.npcInfo.tnpc.npcId) 
                    {
                        if ((_view as IDialogModule).viewStatus == 0 && cache.dialog.taskList.indexOf(loc3.sTask.code) == -1) 
                        {
                            this.taskListReq();
                        }
                    }
                    ++loc1;
                }
            }
            return;
        }

        /* internal  */onTaskUpdataRes(arg1: Array<any>): void
        {
            var loc1=0;
            var loc2=0;
            var loc3=null;
            if (_view && !_view.isHide) 
            {
                loc2 = arg1.length;
                while (loc1 < loc2) 
                {
                    if ((loc3 = arg1[loc1]).status == ETaskStatus._ETaskStatusHadCompleted && loc3.sTask.endNpc == cache.dialog.npcInfo.tnpc.npcId) 
                    {
                        if ((_view as IDialogModule).viewStatus == 0 && cache.dialog.taskList.indexOf(loc3.sTask.code) == -1) 
                        {
                            this.taskListReq();
                        }
                    }
                    ++loc1;
                }
            }
            return;
        }

        /* internal  */onSceneUpdateHandler(arg1: DataEvent): void
        {
            if (_view && !_view.isHide) 
            {
                _view.hide();
            }
            return;
        }

        /* internal  */onUpdateDialogTaskInfo(arg1: DataEvent): void
        {
            if (_view && !_view.isHide) 
            {
                this.checkShowAndUpdateDialog(cache.dialog.npcInfo, cache.dialog.getNPCTalk(), [], cache.dialog.operateList);
            }
            return;
        }

        /* internal  */hideOrShowDialogPanelHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as Boolean;
            if (_view == null) 
            {
                return;
            }
            if (loc1) 
            {
                (_view as IDialogModule).isSetHide = false;
                (_view as IView).show();
            }
            else 
            {
                (_view as IDialogModule).isSetHide = true;
                (_view as IView).hide();
            }
            return;
        }

        /* internal  */guideNextShowingOpListHandler(arg1: DataEvent): void
        {
            this._GuideNextShowingOpData = arg1.data as DialogGuideNextShowingOpData;
            return;
        }

        /* internal  */reqGuildTaskQuickComHandler(arg1: DataEvent): void
        {
            var loc1=null;
            var loc3=null;
            var loc4=false;
            var loc2=Cache.instance.task.getTaskByGroup(ETaskGroup._ETaskGroupGuild);
            var loc5=0;
            var loc6=loc2;
            for(loc3 of loc6) 
            {
                if (loc3.status == ETaskStatus._ETaskStatusHadCompleted) 
                {
                    continue;
                }
                loc4 = false;
                var loc7=loc3.sTask.type;
                switch (loc7) 
                {
                    case ETaskType._ETaskTypeCatchBoss: case ETaskType._ETaskTypeCollect: case ETaskType._ETaskTypeGroupKillBoss: case ETaskType._ETaskTypeKillBoss: case ETaskType._ETaskTypeKillBossCount: case ETaskType._ETaskTypeKillBossDrop: case ETaskType._ETaskTypeKillBossEx: case ETaskType._ETaskTypeKillBossDropEx:
                    {
                        loc1 = loc3;
                        break;
                    }
                    default:
                    {
                        break;
                    }
                }
                if (loc3 == null) 
                {
                    continue;
                }
                break;
            }
            if (loc1 == null) 
            {
                return;
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.TaskQuickComReq, loc1));
            return;
        }

        /* internal  */onGuildTaskInfoRes(arg1: GuildTaskInfo): void
        {
            if (view.isHide) 
            {
                this.openView();
            }
            (_view as IDialogModule).onGuildTaskRes(arg1, cache.dialog.dialogInfo, cache.dialog.npcInfo);
            return;
        }

        /* internal  */onGuildFlameInfoRes(arg1: SGuildFlameInfo=null): void
        {
            if (!(arg1 == null) && _view && !_view.isHide) 
            {
                (_view as IDialogModule).onGuildFlameRes(arg1);
            }
            return;
        }

        /* internal  */onGuildWarCopyInfosUpdate(arg1: Object): void
        {
            if (this.checkDistance()) 
            {
                if (GameMapUtil.curMapState.isCrossCountry) 
                {
                    cache.dialog.updateNpcOperate(cache.dialog.npcInfo.tnpc.effectCross);
                }
                else 
                {
                    cache.dialog.updateNpcOperate(cache.dialog.npcInfo.tnpc.effectDes);
                }
                this.checkShowAndUpdateDialog(cache.dialog.npcInfo, cache.dialog.getNPCTalk(), [], cache.dialog.operateList);
            }
            return;
        }

        /* internal  */checkShowAndUpdateDialog(arg1: NPCInfo, arg2: string, arg3: Array<any>, arg4: Array<any>): void
        {
            if (GameMapUtil.curMapState.isCrossCountry && (arg2 == null || arg2 == "") || arg1.tnpc.npcId == 5010013 || arg1.tnpc.npcId == 5010014) 
            {
                if (!view.isHide) 
                {
                    view.hide();
                }
                return;
            }
            if (view.isHide) 
            {
                view.show();
            }
            (view as IDialogModule).updateTaskList(cache.dialog.npcInfo, cache.dialog.getNPCTalk(), arg3, cache.dialog.operateList);
            return;
        }

        /* internal  */onCopyMagicTowerQuestion(arg1: MessageBlock): void
        {
            var loc2=null;
            var loc1=arg1.messageBase as SStory;
            if (this.checkDistance()) 
            {
                loc2 = NpcEffectRule.getNpcEffect(cache.dialog.operateList, ENpcEffect._EnpcEffectCopyDigitalReset);
                if (loc2) 
                {
                    if (view.isHide) 
                    {
                        view.show();
                        (view as IDialogModule).onCopyMagicTowerQuestion(loc2, loc1.text, cache.dialog.npcInfo);
                    }
                }
            }
            return;
        }

        /* internal  */openView(): void
        {
            if (this.checkDistance()) 
            {
                view.show();
            }
            return;
        }

        /* internal  */onWindowShow(arg1: WindowEvent): void
        {
            NpcEffectRule.registDialogEffectWindow(view);
            return;
        }

        /* internal  */onWindowClose(arg1: WindowEvent): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.NpcWindowClose));
            return;
        }

        /* internal  */onTaskChange(arg1: Array<any>, arg2: Boolean=false): void
        {
            var loc1=null;
            var loc2=false;
            var loc3=null;
            var loc4=null;
            var loc5=0;
            var loc6=0;
            var loc7=false;
            if (cache.guide.petGuide || cache.guide.guideSkillLearn || cache.guide.guideUseDrug || cache.guide.guideApplyGuild || cache.guide.guideEquipRefresh && cache.guide.isGuideEquipRefresh(cache.dialog.npcInfo.tnpc.npcId) || cache.guide.guidePetRefresh && cache.guide.isGuidePetRefresh(cache.dialog.npcInfo.tnpc.npcId) || cache.guide.guidePetMerge && cache.guide.isGuidePetMerge(cache.dialog.npcInfo.tnpc.npcId) || cache.guidePetLifeStyle.isGuideDressLifeStyle) 
            {
                if (!view.isHide) 
                {
                    view.hide();
                }
                return;
            }
            if (arg1.length != 0) 
            {
                loc5 = 0;
                loc6 = arg1.length;
                loc7 = false;
                while (loc5 < loc6) 
                {
                    if ((loc4 = arg1[loc5]).status == ETaskStatus._ETaskStatusCanGet || loc4.status == ETaskStatus._ETaskStatusHadCompleted || loc4.status == ETaskStatus._ETaskStatusHadFail) 
                    {
                        loc7 = true;
                        break;
                    }
                    ++loc5;
                }
                if (loc7) 
                {
                    this.onTaskListRes(arg1);
                }
                else if (!view.isHide) 
                {
                    view.hide();
                }
            }
            else 
            {
                if (!view.isHide) 
                {
                    view.hide();
                }
                if (cache.dialog.operateList.length > 0) 
                {
                    if ((cache.dialog.operateList[0] as OperateInfo).operateId != ENpcEffect._ENpcEffectSectarianTask) 
                    {
                        if ((cache.dialog.operateList[0] as OperateInfo).operateId != ENpcEffect._ENpcEffectCentralTask) 
                        {
                            if ((cache.dialog.operateList[0] as OperateInfo).operateId != ENpcEffect._ENpcEffectTransport) 
                            {
                                if (arg2 && (cache.dialog.operateList[0] as OperateInfo).operateId == ENpcEffect._ENpcEffectHuntTask) 
                                {
                                    if (cache.daily.huntInfo.num < cache.daily.huntInfo.totalNum) 
                                    {
                                        this.taskListReq();
                                    }
                                }
                            }
                            else if (cache.task.hasTaskByGroup(ETaskGroup._ETaskGroupTransport, false)) 
                            {
                                this.taskListReq();
                            }
                        }
                        else if (cache.task.hasTaskByGroup(ETaskGroup._ETaskGroupCycle, false)) 
                        {
                            this.taskListReq();
                        }
                    }
                    else 
                    {
                        loc1 = cache.task.getTaskByGroup(ETaskGroup._ETaskGroupSchool);
                        loc2 = false;
                        var loc8=0;
                        var loc9=loc1;
                        for(loc3 of loc9) 
                        {
                            if (!(loc3.status == ETaskStatus._ETaskStatusHadCompleted || loc3.status == ETaskStatus._ETaskStatusHadFail)) 
                            {
                                continue;
                            }
                            loc2 = true;
                            break;
                        }
                        if (loc2 != true) 
                        {
                            if (loc1.length != 3) 
                            {
                                if (cache.daily.schoolInfo.num < cache.daily.schoolInfo.totalNum) 
                                {
                                    GameManager.instance.popupWindow((cache.dialog.operateList[0] as OperateInfo).operateId);
                                }
                            }
                        }
                        else 
                        {
                            GameManager.instance.popupWindow((cache.dialog.operateList[0] as OperateInfo).operateId);
                        }
                    }
                }
            }
            return;
        }

        /* internal  */checkOutDistance(arg1: Object /* flash.events.Event */): void
        {
            var loc1=null;
            var loc2=0;
            var loc3=null;
            if (cache.dialog.npcInfo && !this.checkDistance()) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.AI_NpcOutDestance, cache.dialog.npcInfo));
                loc1 = ThingUtil.npcUtil.getNpc(cache.dialog.npcInfo.tnpc.npcId);
                if (loc1) 
                {
                    loc1.selected = false;
                }
                while (NpcEffectRule.registedDialogWindow.length > 0) 
                {
                    if ((loc3 = NpcEffectRule.registedDialogWindow[0]) && !loc3.isHide) 
                    {
                        loc3.hide();
                    }
                    if (loc2 > 20) 
                    {
                        break;
                    }
                    ++loc2;
                }
                RolePlayer.instance.removeEventListener(PlayerEvent.GIRD_WALK_END, this.checkOutDistance);
                Dispatcher.removeEventListener(EventName.Convey_Res, this.checkOutDistance);
                Dispatcher.removeEventListener(EventName.ChangeScene, this.checkOutDistance);
            }
            return;
        }

        /* internal  */checkDistance(): Boolean
        {
            return NpcEffectRule.checkDistance(cache.dialog.npcInfo);
        }

        /* internal  */onRoleKillByEntity(arg1: MessageBlock): void
        {
            if (_view && !_view.isHide) 
            {
                _view.hide();
            }
            return;
        }

        /* internal  */eatParadeNpcDialogHide(arg1: DataEvent): void
        {
            if (_view && !_view.isHide && int(arg1.data) == 0) 
            {
                _view.hide();
            }
            return;
        }

        private /* var */_GuideNextShowingOpData: DialogGuideNextShowingOpData;

        private /* var */_operateInfo: OperateInfo;
    }
